<?php

/**
 * Description of XslView
 *
 * @author Ian Clarence <ian.clarence@gmail.com>
 */
final class XslView extends ViewXsl {
    
    /**
     * The constructor.
     */
    public function __construct($fields = array()) {
        parent::__construct($fields);
    }
    
    /**
     * Instance of a panel.
     * 
     * @param array $panel
     * @return mixed
     */
    protected function getPanelInstance($panel) {
        return $this->genPanelView($this->getPanelFields($panel));
    }
    
    /**
     * View fields to be passed to panel view object.
     * 
     * @param array $panel
     * @return array
     */
    protected function getPanelFields($panel) {
        $output = $this->fields;
        unset($output['panels']);
        $output['panel'] = $panel;
        return $output;
    }

    /**
     * Getting the page body for when in normal running mode.
     *
     * @return ViewHtmlElement
     */
    protected function getBody() {
        
        /**
         * Instantiating the panels. 
         */
        $panelViews = array();
        foreach ($this->fields['panels'] as $panelViewKey => $panel) {
            $panelViews[$panelViewKey] = $this->getPanelInstance($panel);
            $panelViews[$panelViewKey]->make();
        }
        
        /**
         * Main container. 
         */
        $container = ViewHtmlElement::elementInit('div', NULL, 'id', 'container');
        
        /**
         * The header. 
         */
        $container->addInnerHtml($panelViews[0]);
        
        /**
         * Homepage. 
         */
        if ($this->fields['page']['page_type'] == 1) {        
            $container->addInnerHtml($panelViews[1]);        
            $container->addInnerHtml($panelViews[2]);            
        }
        
        /**
         * Other pages. 
         */
        else {
        
            /**
            * Subcontainer containing left navigation and page content. 
            */
            $subContainer = ViewHtmlElement::elementInit('div', NULL, 'id', 'subcontainer');        
            $subContainer->addInnerHtml($panelViews[1]);        
            $subContainer->addInnerHtml($panelViews[2]);

            /**
            * The footer. 
            */
            $subContainer->addInnerHtml($panelViews[3]);        
            $container->addInnerHtml($subContainer);
        }
        
        /**
         * Showing the template variables.
         * Only possible in development. 
         */
        if (SERVER == DEVELOPMENT_SERVER && SHOW_TEMPLATE_VARIABLES == YES) {
            $variables = ViewHtmlElement::elementInit('div', NULL, 'id', 'variables');
            $variables->addBr();
            $pre = $variables->addPre(print_r($this->fields, TRUE));
            $container->addInnerHtml($variables);
        }
        
        /**
         * Returning the body. 
         */
        $body = $container->encapsulate('body');
        return $body;
    }

}